home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / c_tutor.arc / TEXT.ARC / CHAP3.TXT < prev    next >
Text File  |  1990-08-08  |  19KB  |  455 lines

  1.  
  2.                         Chapter 3 - Program Control
  3.  
  4.  
  5.                                THE WHILE LOOP
  6.  
  7.              The  C programming language has several structures  for
  8.         looping  and conditional branching.   We will cover them all
  9.         in this chapter and we will begin with the while loop.   The
  10.         while  loop continues to loop while some condition is  true.
  11.         When   the   condition  becomes  false,   the   looping   is
  12.         discontinued.   It therefore does just what it says it does,
  13.         the name of the loop being very descriptive.
  14.  
  15.              Load the program WHILE.C and display it for an  example
  16.         of  a while loop.   We begin with a comment and the  program
  17.         name,  then  go  on  to define an integer  variable  "count"
  18.         within the body of the program.  The variable is set to zero
  19.         and we come to the while loop itself.  The syntax of a while
  20.         loop is just as shown here.  The keyword "while" is followed
  21.         by an expression of something in parentheses,  followed by a
  22.         compound  statement  bracketed by braces.   As long  as  the
  23.         expression in parenthesis is true, all statements within the
  24.         braces will be executed.   In this case,  since the variable
  25.         count  is incremented by one every time the  statements  are
  26.         executed, it will eventually reach 6, the statement will not
  27.         be executed,  and the loop will be terminated.   The program
  28.         control   will   resume  at  the  statement  following   the
  29.         statements in braces.
  30.  
  31.              We  will  cover  the compare  expression,  the  one  in
  32.         parentheses, in the next chapter.  Until then, simply accept
  33.         the  expressions for what you think they should do  and  you
  34.         will probably be correct.
  35.  
  36.              Several  things must be pointed out regarding the while
  37.         loop.   First,  if the variable count were initially set  to
  38.         any  number greater than 5,  the statements within the  loop
  39.         would  not be executed at all,  so it is possible to have  a
  40.         while  loop  that  never  is  executed.   Secondly,  if  the
  41.         variable  were  not incremented in the loop,  then  in  this
  42.         case,  the loop would never terminate, and the program would
  43.         never complete.   Finally, if there is only one statement to
  44.         be executed within the loop, it does not need braces but can
  45.         stand alone.
  46.  
  47.              Compile and run this program.
  48.  
  49.                              THE DO-WHILE LOOP
  50.  
  51.              A  variation  of the while loop is illustrated  in  the
  52.         program DOWHILE.C,  which you should load and display.  This
  53.         program  is nearly identical to the last one except that the
  54.         loop  begins  with the reserved word  "do",  followed  by  a
  55.         compound  statement  in  braces,   then  the  reserved  word
  56.  
  57.  
  58.                                  Page 13
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                         Chapter 3 - Program Control
  69.  
  70.  
  71.         "while",  and  finally  an expression in  parentheses.   The
  72.         statements in the braces are executed repeatedly as long  as
  73.         the expression in parentheses is true.   When the expression
  74.         in parentheses becomes false,  execution is terminated,  and
  75.         control passes to the statements following this statement.
  76.  
  77.              Several  things  must  be pointed  out  regarding  this
  78.         statement.  Since  the test is done at the end of the  loop,
  79.         the  statements  in  the braces will always be  executed  at
  80.         least once.   Secondly,  if "i" were not changed within  the
  81.         loop,  the loop would never terminate, and hence the program
  82.         would  never terminate.   Finally,  just like for the  while
  83.         loop,  if  only  one statement will be executed  within  the
  84.         loop,  no braces are required.  Compile and run this program
  85.         to see if it does what you think it should do.
  86.  
  87.              It  should come as no surprise to you that these  loops
  88.         can be nested.  That is, one loop can be included within the
  89.         compound  statement of another loop,  and the nesting  level
  90.         has no limit.
  91.  
  92.                                 THE FOR LOOP
  93.  
  94.              The  "for" loop is really nothing new,  it is simply  a
  95.         new  way  to describe the "while" loop.   Load and edit  the
  96.         file  named  FORLOOP.C for an example of a  program  with  a
  97.         "for"  loop.  The  "for" loop consists of the reserved  word
  98.         "for" followed by a rather large expression in  parentheses.
  99.         This expression is really composed of three fields separated
  100.         by  semi-colons.   The  first field contains the  expression
  101.         "index  = 0" and is an initializing field.   Any expressions
  102.         in  this field are executed prior to the first pass  through
  103.         the loop.   There is essentially no limit as to what can  go
  104.         here,  but  good programming practice would require it to be
  105.         kept simple.   Several initializing statements can be placed
  106.         in this field, separated by commas.
  107.  
  108.              The second field,  in this case containing "index < 6",
  109.         is  the  test which is done at the beginning  of  each  loop
  110.         through  the program.   It can be any expression which  will
  111.         evaluate  to a true or false.   (More will be said about the
  112.         actual value of true and false in the next chapter.)
  113.  
  114.              The expression contained in the third field is executed
  115.         each time the loop is executed but it is not executed  until
  116.         after  those  statements  in the main body of the  loop  are
  117.         executed.   This field, like the first, can also be composed
  118.         of several operations separated by commas.
  119.  
  120.              Following  the  for()  expression  is  any  single   or
  121.         compound statement which will be executed as the body of the
  122.  
  123.  
  124.                                  Page 14
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                         Chapter 3 - Program Control
  135.  
  136.  
  137.         loop.   A  compound  statement  is  any  group  of  valid  C
  138.         statements enclosed in braces.   In nearly any context in C,
  139.         a  simple statement can be replaced by a compound  statement
  140.         that will be treated as if it were a single statement as far
  141.         as program control goes.  Compile and run this program.
  142.  
  143.              You  may  be  wondering why there  are  two  statements
  144.         available that do exactly the same thing because the "while"
  145.         and  the "for" loop do exactly the same thing.  The  "while"
  146.         is convenient to use for a loop that you don't have any idea
  147.         how many times the loop will be executed, and the "for" loop
  148.         is  usually used in those cases when you are doing  a  fixed
  149.         number  of  iterations.  The "for" loop is  also  convenient
  150.         because  it moves all of the control information for a  loop
  151.         into one place, between the parentheses, rather than at both
  152.         ends  of the code.  It is your choice as to which you  would
  153.         rather use.
  154.  
  155.                               THE IF STATEMENT
  156.  
  157.              Load  and  display the file IFELSE.C for an example  of
  158.         our first conditional branching statement, the "if".  Notice
  159.         first,  that there is a "for" loop with a compound statement
  160.         as its executable part containing two "if" statements.  This
  161.         is an example of how statements can be nested.  It should be
  162.         clear  to  you  that each of the  "if"  statements  will  be
  163.         executed 10 times.
  164.  
  165.              Consider the first "if" statement.   It starts with the
  166.         keyword  "if" followed by an expression in parentheses.   If
  167.         the expression is evaluated and found to be true, the single
  168.         statement following the "if" is executed,  and if false, the
  169.         following  statement  is  skipped.   Here  too,  the  single
  170.         statement  can be replaced by a compound statement  composed
  171.         of  several statements bounded by  braces.   The  expression
  172.         "data  == 2" is simply asking if the value of data is  equal
  173.         to 2,  this will be explained in detail in the next chapter.
  174.         (Simply suffice for now that if "data = 2" were used in this
  175.         context, it would mean a completely different thing.)
  176.  
  177.                             NOW FOR THE IF-ELSE
  178.  
  179.              The  second  "if"  is  similar to the  first  with  the
  180.         addition  of a new reserved word,  the "else" following  the
  181.         first  printf  statement.   This  simply says  that  if  the
  182.         expression in the parentheses evaluates as true,  the  first
  183.         expression  is executed,  otherwise the expression following
  184.         the "else" is executed.   Thus,  one of the two  expressions
  185.         will  always be executed,  whereas in the first example  the
  186.         single expression was either executed or skipped.  Both will
  187.  
  188.  
  189.  
  190.                                  Page 15
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.                         Chapter 3 - Program Control
  201.  
  202.  
  203.         find  many uses in your C programming efforts.   Compile and
  204.         run this program to see if it does what you expect.
  205.  
  206.                            THE BREAK AND CONTINUE
  207.  
  208.              Load  the file named BREAKCON.C for an example  of  two
  209.         new statements.  Notice that in the first "for", there is an
  210.         if  statement that calls a break if xx equals 8.   The break
  211.         will  jump  out of the loop you are in and  begin  executing
  212.         statements following the loop,  effectively terminating  the
  213.         loop.   This  is a valuable statement when you need to  jump
  214.         out  of  a  loop  depending on the  value  of  some  results
  215.         calculated  in the loop.   In this case,  when xx reaches 8,
  216.         the  loop is terminated and the last value printed  will  be
  217.         the previous value, namely 7.
  218.  
  219.              The  next  "for" loop,  contains a  continue  statement
  220.         which  does not cause termination of the loop but jumps  out
  221.         of the present iteration.  When the value of xx reaches 8 in
  222.         this case,  the program will jump to the end of the loop and
  223.         continue  executing  the loop,  effectively eliminating  the
  224.         printf statement during the pass through the loop when xx is
  225.         eight.   Compile and run the program to see if it does  what
  226.         you expect.
  227.  
  228.                             THE SWITCH STATEMENT
  229.  
  230.              Load  and  display the file SWITCH.C for an example  of
  231.         the  biggest construct yet in the C  language,  the  switch.
  232.         The switch is not difficult, so don't let it intimidate you.
  233.         It  begins with the keyword "switch" followed by a  variable
  234.         in parentheses which is the switching variable, in this case
  235.         "truck".   As many cases as desired are then enclosed within
  236.         a pair of braces.  The reserved word "case" is used to begin
  237.         each  case  entered followed by the value of  the  variable,
  238.         then a colon, and the statements to be executed.
  239.  
  240.              In  this example,  if the variable "truck" contains the
  241.         value 3 during this pass of the switch statement, the printf
  242.         will  cause "The value is three" to be  displayed,  and  the
  243.         "break" statement will cause us to jump out of the switch.
  244.  
  245.              Once  an  entry  point is  found,  statements  will  be
  246.         executed until a "break" is found or until the program drops
  247.         through  the bottom of the switch braces.   If the  variable
  248.         has  the value 5,  the statements will begin executing where
  249.         "case  5  :" is found,  but the first statements  found  are
  250.         where the case 8 statements are.  These are executed and the
  251.         break  statement  in the "case 8" portion  will  direct  the
  252.         execution  out the bottom of the switch.   The various  case
  253.  
  254.  
  255.  
  256.                                  Page 16
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.                         Chapter 3 - Program Control
  267.  
  268.  
  269.         values can be in any order and if a value is not found,  the
  270.         default portion of the switch will be executed.
  271.  
  272.              It should be clear that any of the above constructs can
  273.         be  nested  within  each  other  or  placed  in  succession,
  274.         depending on the needs of the particular programming project
  275.         at hand.
  276.  
  277.              Compile  and  run SWITCH.C to see if it does  what  you
  278.         expect it to after this discussion.
  279.  
  280.              Load  and display the file GOTOEX.C for an example of a
  281.         file  with some "goto" statements in it.   To use  a  "goto"
  282.         statement,  you simply use the reserved word "goto" followed
  283.         by the symbolic name to which you wish to jump.  The name is
  284.         then  placed  anywhere in the program followed by  a  colon.
  285.         You  are  not  allowed to jump into any loop,  but  you  are
  286.         allowed to jump out of a loop.  Also, you are not allowed to
  287.         jump out of any function into another.   These attempts will
  288.         be  flagged  by  your Turbo C compiler as an  error  if  you
  289.         attempt any of them.
  290.  
  291.              This  particular program is really a mess but it  is  a
  292.         good example of why software writers are trying to eliminate
  293.         the  use of the "goto" statement as much as  possible.   The
  294.         only place in this program where it is reasonable to use the
  295.         "goto"  is the one in line 18 where the program jumps out of
  296.         the three nested loops in one jump.   In this case it  would
  297.         be  rather messy to set up a variable and jump  successively
  298.         out of all three loops but one "goto" statement gets you out
  299.         of all three.
  300.  
  301.              Some  persons say the "goto" statement should never  be
  302.         used  under  any circumstances, but this  is  rather  narrow
  303.         minded  thinking.  If there is a place where a  "goto"  will
  304.         clearly do a neater control flow than some other  construct,
  305.         feel free to use it.  It should not be abused however, as it
  306.         is in the rest of the program on your monitor.
  307.  
  308.              Entire  books  are written on  "gotoless"  programming,
  309.         better known as Structured Programming.   These will be left
  310.         to  your  study.   One  point  of reference  is  the  Visual
  311.         Calculator described in Chapter 14 of this  tutorial.   This
  312.         program  is  contained in four separately compiled  programs
  313.         and  is a rather large complex program.   If you spend  some
  314.         time studying the source code,  you will find that there  is
  315.         not  a single "goto" statement anywhere in it.   Compile and
  316.         run  GOTOEX.C  and study its output.   It would  be  a  good
  317.         exercise  to rewrite it and see how much more readable it is
  318.         when the statements are listed in order.
  319.  
  320.  
  321.  
  322.                                  Page 17
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.                         Chapter 3 - Program Control
  333.  
  334.  
  335.                        FINALLY, A MEANINGFUL PROGRAM
  336.  
  337.              Load  the  file  named TEMPCONV.C for an example  of  a
  338.         useful,  even  though somewhat limited program.   This is  a
  339.         program  that generates a list of centigrade  and fahrenheit
  340.         temperatures  and prints a message out at the freezing point
  341.         of water and another at the boiling point of water.
  342.  
  343.              Of particular importance is the formatting.  The header
  344.         is  simply  several lines of comments  describing  what  the
  345.         program  does in a manner that catches the readers attention
  346.         and  is  still  pleasing to the  eye.  You  will  eventually
  347.         develop your own formatting style, but this is a good way to
  348.         start.   Also if you observe the for loop,  you will  notice
  349.         that  all  of  the contents of the  compound  statement  are
  350.         indented  3 spaces to the right of the "for" reserved  word,
  351.         and  the  closing brace is lined up under the "f" in  "for".
  352.         This  makes debugging a bit easier because the  construction
  353.         becomes  very  obvious.   You  will  also  notice  that  the
  354.         "printf"  statements that are in the "if" statements  within
  355.         the  big  "for" loop are indented  three  additional  spaces
  356.         because they are part of another construct.
  357.  
  358.              This  is  the first program in which we used more  than
  359.         one  variable.   The three variables are simply  defined  on
  360.         three  different lines and are used in the same manner as  a
  361.         single variable was used in previous programs.   By defining
  362.         them  on different lines,  we have an opportunity to  define
  363.         each with a comment.
  364.  
  365.                       ANOTHER POOR PROGRAMMING EXAMPLE
  366.  
  367.              Recalling  UGLYFORM.C from the last chapter,  you saw a
  368.         very  poorly  formatted program.   If you load  and  display
  369.         DUMBCONV.C you will have an example of poor formatting which
  370.         is  much closer to what you will find in practice.  This  is
  371.         the same program as TEMPCONV.C with the comments removed and
  372.         the variable names changed to remove the descriptive  aspect
  373.         of  the names.  Although this program does exactly the  same
  374.         as  the  last  one, it is much more difficult  to  read  and
  375.         understand.   You should begin to develop  good  programming
  376.         practices now.
  377.  
  378.              Compile  and  run  this program to  see  that  it  does
  379.         exactly what the last one did.
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.                                  Page 18
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.                         Chapter 3 - Program Control
  399.  
  400.  
  401.         PROGRAMMING EXERCISES
  402.  
  403.         1.  Write a program that writes your name on the monitor ten
  404.             times.   Write this program three times, once with  each
  405.             looping method.
  406.  
  407.         2.  Write a program that counts from one to ten, prints the
  408.             values  on  a separate line for each,  and  includes  a
  409.             message  of  your  choice when the count  is  3  and  a
  410.             different message when the count is 7.
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.                                  Page 19
  455.